home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / readline / signals.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  8KB  |  313 lines

  1. /* Modified by Klaus Gebhardt, October 1996 */
  2. /* signals.c -- signal handling support for readline. */
  3.  
  4. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  5.  
  6.    This file is part of the GNU Readline Library, a library for
  7.    reading lines of text with interactive input and history editing.
  8.  
  9.    The GNU Readline Library is free software; you can redistribute it
  10.    and/or modify it under the terms of the GNU General Public License
  11.    as published by the Free Software Foundation; either version 1, or
  12.    (at your option) any later version.
  13.  
  14.    The GNU Readline Library is distributed in the hope that it will be
  15.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  16.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    The GNU General Public License is often shipped with GNU software, and
  20.    is generally kept in a file called COPYING or LICENSE.  If you do not
  21.    have a copy of the license, write to the Free Software Foundation,
  22.    675 Mass Ave, Cambridge, MA 02139, USA. */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27.  
  28. #define READLINE_LIBRARY
  29.  
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <fcntl.h>
  33. #if defined (HAVE_SYS_FILE)
  34. #  include <sys/file.h>
  35. #endif /* HAVE_SYS_FILE */
  36. #include <signal.h>
  37.  
  38. #if defined (HAVE_UNISTD_H)
  39. #  include <unistd.h>
  40. #endif /* HAVE_UNISTD_H */
  41.  
  42. #if defined (HAVE_STDLIB_H)
  43. #  include <stdlib.h>
  44. #else
  45. #  include "ansi_stdlib.h"
  46. #endif /* HAVE_STDLIB_H */
  47.  
  48. #include <errno.h>
  49. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  50. #if !defined (errno)
  51. extern int errno;
  52. #endif /* !errno */
  53.  
  54. #include "posixstat.h"
  55.  
  56. /* System-specific feature definitions and include files. */
  57. #include "rldefs.h"
  58.  
  59. #if defined (GWINSZ_IN_SYS_IOCTL)
  60. #  include <sys/ioctl.h>
  61. #endif /* GWINSZ_IN_SYS_IOCTL */
  62.  
  63. /* Some standard library routines. */
  64. #include "readline.h"
  65. #include "history.h"
  66.  
  67. extern int readline_echoing_p;
  68. extern int rl_pending_input;
  69. extern int _rl_meta_flag;
  70.  
  71. extern void free_undo_list ();
  72.  
  73. /* This typedef is equivalant to the one for Function; it allows us
  74.    to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  75. typedef RETSIGTYPE SigHandler ();
  76.  
  77.  
  78. #if defined (__GO32__)
  79. #  undef HANDLE_SIGNALS
  80. #endif /* __GO32__ */
  81.  
  82. #if defined (STATIC_MALLOC)
  83. static char *xmalloc (), *xrealloc ();
  84. #else
  85. extern char *xmalloc (), *xrealloc ();
  86. #endif /* STATIC_MALLOC */
  87.  
  88.  
  89. /* **************************************************************** */
  90. /*                                        */
  91. /*               Signal Handling                          */
  92. /*                                    */
  93. /* **************************************************************** */
  94.  
  95. #if defined (SIGWINCH)
  96. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  97.  
  98. static RETSIGTYPE
  99. rl_handle_sigwinch (sig)
  100.      int sig;
  101. {
  102.   if (readline_echoing_p)
  103.     {
  104.       _rl_set_screen_size (fileno (rl_instream), 1);
  105.       _rl_redisplay_after_sigwinch ();
  106.     }
  107.  
  108.   if (old_sigwinch &&
  109.       old_sigwinch != (SigHandler *)SIG_IGN &&
  110.       old_sigwinch != (SigHandler *)SIG_DFL)
  111.     (*old_sigwinch) (sig);
  112. #if RETSIGTYPE != void
  113.   return (0);
  114. #endif
  115. }
  116. #endif  /* SIGWINCH */
  117.  
  118. #if defined (HANDLE_SIGNALS)
  119. /* Interrupt handling. */
  120. static SigHandler
  121.   *old_int  = (SigHandler *)NULL,
  122. #if defined (__EMX__)
  123.   *old_break= (SigHandler *)NULL,
  124.   *old_term = (SigHandler *)NULL,
  125. #endif
  126.   *old_alrm = (SigHandler *)NULL;
  127. #if !defined (SHELL)
  128. static SigHandler
  129.   *old_tstp = (SigHandler *)NULL,
  130.   *old_ttou = (SigHandler *)NULL,
  131.   *old_ttin = (SigHandler *)NULL,
  132.   *old_cont = (SigHandler *)NULL;
  133. #endif /* !SHELL */
  134.  
  135. /* Handle an interrupt character. */
  136. static RETSIGTYPE
  137. rl_signal_handler (sig)
  138.      int sig;
  139. {
  140. #if defined (HAVE_POSIX_SIGNALS)
  141.   sigset_t set;
  142. #else /* !HAVE_POSIX_SIGNALS */
  143. #  if defined (HAVE_BSD_SIGNALS)
  144.   long omask;
  145. #  endif /* HAVE_BSD_SIGNALS */
  146. #endif /* !HAVE_POSIX_SIGNALS */
  147.  
  148. #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
  149.   /* Since the signal will not be blocked while we are in the signal
  150.      handler, ignore it until rl_clear_signals resets the catcher. */
  151. #if ! defined (__EMX__)
  152.   if (sig == SIGINT)
  153. #else
  154.   if ((sig == SIGINT) || (sig == SIGBREAK))
  155. #endif
  156.     signal (sig, SIG_IGN);
  157. #endif /* !HAVE_BSD_SIGNALS */
  158.  
  159.   switch (sig)
  160.     {
  161.     case SIGINT:
  162. #if defined (__EMX__)
  163.     case SIGBREAK:
  164. #endif
  165.       {
  166.     register HIST_ENTRY *entry;
  167.  
  168.     free_undo_list ();
  169.  
  170.     entry = current_history ();
  171.     if (entry)
  172.       entry->data = (char *)NULL;
  173.       }
  174.       _rl_kill_kbd_macro ();
  175.       rl_clear_message ();
  176.       rl_init_argument ();
  177.  
  178. #if defined (SIGTSTP)
  179.     case SIGTSTP:
  180.     case SIGTTOU:
  181.     case SIGTTIN:
  182. #endif /* SIGTSTP */
  183.     case SIGALRM:
  184.       rl_clean_up_for_exit ();
  185.       rl_deprep_terminal ();
  186.       rl_clear_signals ();
  187.       rl_pending_input = 0;
  188.  
  189. #if defined (__EMX__)
  190.       signal (sig, SIG_ACK);
  191. #endif
  192.  
  193. #if defined (HAVE_POSIX_SIGNALS)
  194.       sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set);
  195.       sigdelset (&set, sig);
  196. #else /* !HAVE_POSIX_SIGNALS */
  197. #  if defined (HAVE_BSD_SIGNALS)
  198.       omask = sigblock (0);
  199. #  endif /* HAVE_BSD_SIGNALS */
  200. #endif /* !HAVE_POSIX_SIGNALS */
  201.  
  202.       kill (getpid (), sig);
  203.  
  204.       /* Let the signal that we just sent through.  */
  205. #if defined (HAVE_POSIX_SIGNALS)
  206.       sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
  207. #else /* !HAVE_POSIX_SIGNALS */
  208. #  if defined (HAVE_BSD_SIGNALS)
  209.       sigsetmask (omask & ~(sigmask (sig)));
  210. #  endif /* HAVE_BSD_SIGNALS */
  211. #endif /* !HAVE_POSIX_SIGNALS */
  212.  
  213.       rl_prep_terminal (_rl_meta_flag);
  214.       rl_set_signals ();
  215.     }
  216.  
  217. #if RETSIGTYPE != void
  218.   return (0);
  219. #endif
  220. }
  221.  
  222. #if defined (HAVE_POSIX_SIGNALS)
  223. static SigHandler *
  224. rl_set_sighandler (sig, handler)
  225.      int sig;
  226.      SigHandler *handler;
  227. {
  228.   struct sigaction act, oact;
  229.  
  230.   act.sa_handler = handler;
  231.   act.sa_flags = 0;
  232.   sigemptyset (&act.sa_mask);
  233.   sigemptyset (&oact.sa_mask);
  234.   sigaction (sig, &act, &oact);
  235.   return (oact.sa_handler);
  236. }
  237.  
  238. #else /* !HAVE_POSIX_SIGNALS */
  239. #  define rl_set_sighandler(sig, handler) (SigHandler *)signal (sig, handler)
  240. #endif /* !HAVE_POSIX_SIGNALS */
  241.  
  242. rl_set_signals ()
  243. {
  244.   old_int = (SigHandler *)rl_set_sighandler (SIGINT, rl_signal_handler);
  245.   if (old_int == (SigHandler *)SIG_IGN)
  246.     rl_set_sighandler (SIGINT, SIG_IGN);
  247.  
  248. #if defined (__EMX__)
  249.   old_break = (SigHandler *)rl_set_sighandler (SIGBREAK, rl_signal_handler);
  250.   if (old_break == (SigHandler *)SIG_IGN)
  251.     signal (SIGBREAK, SIG_IGN);
  252. #endif
  253.  
  254.   old_alrm = (SigHandler *)rl_set_sighandler (SIGALRM, rl_signal_handler);
  255.   if (old_alrm == (SigHandler *)SIG_IGN)
  256.     rl_set_sighandler (SIGALRM, SIG_IGN);
  257.  
  258. #if !defined (SHELL)
  259.  
  260. #if defined (SIGTSTP)
  261.   old_tstp = (SigHandler *)rl_set_sighandler (SIGTSTP, rl_signal_handler);
  262.   if (old_tstp == (SigHandler *)SIG_IGN)
  263.     rl_set_sighandler (SIGTSTP, SIG_IGN);
  264. #endif /* SIGTSTP */
  265. #if defined (SIGTTOU)
  266.   old_ttou = (SigHandler *)rl_set_sighandler (SIGTTOU, rl_signal_handler);
  267.   old_ttin = (SigHandler *)rl_set_sighandler (SIGTTIN, rl_signal_handler);
  268.  
  269.   if (old_tstp == (SigHandler *)SIG_IGN)
  270.     {
  271.       rl_set_sighandler (SIGTTOU, SIG_IGN);
  272.       rl_set_sighandler (SIGTTIN, SIG_IGN);
  273.     }
  274. #endif /* SIGTTOU */
  275.  
  276. #endif /* !SHELL */
  277.  
  278. #if defined (SIGWINCH)
  279.   old_sigwinch =
  280.     (SigHandler *) rl_set_sighandler (SIGWINCH, rl_handle_sigwinch);
  281. #endif /* SIGWINCH */
  282.   return 0;
  283. }
  284.  
  285. rl_clear_signals ()
  286. {
  287.   rl_set_sighandler (SIGINT, old_int);
  288. #if defined (__EMX__)
  289.   rl_set_sighandler (SIGBREAK, old_break);
  290. #endif
  291.   rl_set_sighandler (SIGALRM, old_alrm);
  292.  
  293. #if !defined (SHELL)
  294.  
  295. #if defined (SIGTSTP)
  296.   rl_set_sighandler (SIGTSTP, old_tstp);
  297. #endif
  298.  
  299. #if defined (SIGTTOU)
  300.   rl_set_sighandler (SIGTTOU, old_ttou);
  301.   rl_set_sighandler (SIGTTIN, old_ttin);
  302. #endif /* SIGTTOU */
  303.  
  304. #endif /* !SHELL */
  305.  
  306. #if defined (SIGWINCH)
  307.   rl_set_sighandler (SIGWINCH, old_sigwinch);
  308. #endif
  309.  
  310.   return 0;
  311. }
  312. #endif  /* HANDLE_SIGNALS */
  313.